*********************************************************************************************
*Replication code for Oliver, Steven, Ryan Jablonski, Justin Hastings. 
*"The Tortuga Disease: The Perverse Effects of Illicit Wealth" Forthcoming International Studies Quarterly (Accepted May 2016)
*Creates Figures B3 in the appendix, which shows the estimated variation in CPI density from imputing missing price data using Amelia. 
*Version 10 June, 2016
*********************************************************************************************

clear *
cd "<directory location>"


forvalues i=1/5 {
display `i'

use "cpidata\somaliamarketdata`i'.dta", clear


* calculate CPI
g temp_puntland1 = 95*redsorghum_puntland
g temp_somaliland1 = 95*redsorghum_somaliland
g temp_puntland2 = 3.75*wheatflour_puntland
g temp_somaliland2 = 3.75*wheatflour_somaliland
g temp_puntland3 = 5*sugar_puntland
g temp_somaliland3 = 5*sugar_somaliland
g temp_puntland4 = 4*vegetable_oil_puntland
g temp_somaliland4 = 4*vegetable_oil_somaliland
g temp_puntland5 = 20*camel_milk_puntland
g temp_somaliland5 = 20*camel_milk_somaliland
g temp_puntland6 = 10*min(goat_local_puntland, cattle_local_puntland, camel_local_puntland)
g temp_somaliland6 = 10*min(goat_local_somaliland, cattle_local_somaliland, camel_local_somaliland)
g temp_puntland7 = 4*cowpeas_puntland
g temp_somaliland7 = 4*cowpeas_somaliland
g temp_puntland8 = 1.5*kerosene_puntland
g temp_somaliland8 = 1.5*kerosene_somaliland
g temp_puntland9 = 5*water_drum_puntland
g temp_somaliland9 = 5*water_drum_somaliland

tsset month2

g cmb_puntland = temp_puntland1+temp_puntland2+temp_puntland3+temp_puntland4+temp_puntland5+temp_puntland6+temp_puntland7+temp_puntland8+temp_puntland9
g cmb_somaliland = temp_somaliland1+temp_somaliland2+temp_somaliland3+temp_somaliland4+temp_somaliland5+temp_somaliland6+temp_somaliland7+temp_somaliland8+temp_somaliland9

sum cmb_puntland if month2==1117
local cmb_puntland_march = r(mean)
sum cmb_somaliland if month2==1117
 local cmb_somaliland_march = r(mean)
g cpi_puntland = (cmb_puntland)/(`cmb_puntland_march')*100
g cpi_somaliland = (cmb_somaliland)/(`cmb_somaliland_march')*100

local somaliland_cpi_2005 = 100
local puntland_cpi_2005 = 100

local us_cpi_2005 = 198.3
g real_avg_slsh_usd = avg_slsh_usd * ((`somaliland_cpi_2005'/cpi_somaliland)/(`us_cpi_2005'/us_cpi))
g real_avg_sosh_usd = avg_sosh_usd * ((`puntland_cpi_2005'/cpi_puntland)/(`us_cpi_2005'/us_cpi))

g real_avg_usd_slsh = (1/(avg_slsh_usd/1000)) * ((`us_cpi_2005'/us_cpi)/(`somaliland_cpi_2005'/cpi_somaliland))
g real_avg_usd_sosh = (1/(avg_sosh_usd/1000)) * ((`us_cpi_2005'/us_cpi)/(`puntland_cpi_2005'/cpi_puntland))


keep  cpi_somaliland  cpi_puntland cmb_somaliland cmb_puntland real* month2
sort month2
rename cpi_somaliland cpi_somaliland`i' 
rename cpi_puntland cpi_puntland`i'
rename cmb_puntland cmb_puntland`i'
rename cmb_somaliland cmb_somaliland`i'
rename real_avg_slsh_usd real_avg_slsh_usd`i'
rename real_avg_sosh_usd real_avg_sosh_usd`i'
rename real_avg_usd_slsh real_avg_usd_slsh`i'
rename real_avg_usd_sosh real_avg_usd_sosh`i'
sort month2
save d:\data\piracy_costs_orig\cpidata_imputed`i'.dta, replace
}


use d:\data\piracy_costs_orig\cpidata_imputed1.dta, clear
merge month2 using d:\data\piracy_costs_orig\cpidata_imputed2.dta
drop _merge
sort month2
merge month2 using d:\data\piracy_costs_orig\cpidata_imputed3.dta
drop _merge
sort month2
merge month2 using d:\data\piracy_costs_orig\cpidata_imputed4.dta
drop _merge
sort month2
merge month2 using d:\data\piracy_costs_orig\cpidata_imputed5.dta
drop _merge
sort month2

twoway (kdensity cpi_puntland1) (kdensity cpi_puntland2)(kdensity cpi_puntland3)(kdensity cpi_puntland4)(kdensity cpi_puntland5)
graph export "Figure B3.tif", width(3000) replace

